package com.shangxia.monomer.study.designPatterns.creationType.prototype;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: monomer
 * @ClassName: PrototypeDemo
 * @Time: 2021/7/19
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class PrototypeDemo {

    /**
     * @param args
     * @throws CloneNotSupportedException
     * @Description: 原型模式
     * Specify the kinds of objects to create using a prototypical instance,and create new objects by copying this prototype.
     * （用原型实例指定创建对象的种类，并且通过拷贝这些原型创建新的对象。）
     *  采用克隆实现，注意深浅克隆
     */
    public static void main(String[] args) throws CloneNotSupportedException {

        Original original = new Original("li", "zhang", 11, new Son("wang"));
//        Original clone = (Original) original.clone();
        Original clone = JSONObject.parseObject(JSONObject.toJSONString(original), Original.class);
        System.out.println(original.hashCode() + "  " + original.toString());
        System.out.println(clone.hashCode() + "  " + clone.toString());
        System.out.println();
        clone.getSon().setName("WANG");
        System.out.println(original.hashCode() + "  " + original.toString());
        System.out.println(clone.hashCode() + "  " + clone.toString());
    }
}